]>
Commit | Line | Data |
---|---|---|
1 | // | |
2 | // FlatBezelObjectCell.swift | |
3 | // Flat Bezel | |
4 | // | |
5 | // Created by Ruben Beltran del Rio on 2/8/23. | |
6 | // Copyright © 2023 BRNBW. All rights reserved. | |
7 | // | |
8 | /* | |
9 | import Foundation | |
10 | import Cocoa | |
11 | ||
12 | class FlatBezelObjectCell: QSObjectCell { | |
13 | let preferredImagePosition: NSControl.ImagePosition = .imageAbove | |
14 | ||
15 | override func draw(withFrame cellFrame: NSRect, in controlView: NSView!) { | |
16 | let isFirstResponder = controlView.window?.firstResponder == controlView && !controlView.isKind(of: NSTableView.self) | |
17 | ||
18 | let dropTarget = self.isHighlighted && self.highlightsBy.contains(NSCell.StyleMask.changeBackgroundCellMask) && !self.isBezeled | |
19 | ||
20 | var fillColor: NSColor = self.backgroundColor ?? .textBackgroundColor | |
21 | if (isFirstResponder) { | |
22 | fillColor = self.highlightColor() | |
23 | } | |
24 | if (dropTarget) { | |
25 | fillColor = NSColor(red: 0.77, green: 0.91, blue: 0.96, alpha: 1) | |
26 | } | |
27 | ||
28 | var strokeColor: NSColor = .clear | |
29 | ||
30 | fillColor.setFill() | |
31 | strokeColor.setStroke() | |
32 | ||
33 | let roundRect = NSBezierPath() | |
34 | roundRect.appendRoundedRect(cellFrame, xRadius: cellRadiusFactor(), yRadius: cellRadiusFactor()) | |
35 | roundRect.fill() | |
36 | ||
37 | self.drawInterior(withFrame: self.drawingRect(forBounds: cellFrame), in: controlView) | |
38 | } | |
39 | ||
40 | override func titleRect(forBounds rect: NSRect) -> NSRect { | |
41 | super.titleRect(forBounds: rect.offsetBy(dx: 0, dy: -4)) | |
42 | } | |
43 | ||
44 | override func drawText(for drawObject: QSObject!, withFrame cellFrame: NSRect, in controlView: NSView!) { | |
45 | if self.imagePosition == .imageOnly { | |
46 | return; | |
47 | } | |
48 | ||
49 | var abbrString: String? = nil | |
50 | if controlView.responds(to: #selector(QSSearchObjectView.matchedString)) { | |
51 | abbrString = (controlView as! QSSearchObjectView).matchedString() | |
52 | } | |
53 | ||
54 | var nameString: String? = drawObject.displayName() | |
55 | var hitMask: AutoreleasingUnsafeMutablePointer<NSIndexSet?>? = nil | |
56 | ||
57 | var ranker = drawObject.ranker() | |
58 | if let ranker, let abbrString { | |
59 | nameString = ranker.matchedString(forAbbreviation: abbrString, hitmask: hitMask, inContext: nil) | |
60 | } | |
61 | ||
62 | var rankedStringIsName = nameString == drawObject.displayName() | |
63 | if nameString == nil { | |
64 | nameString = drawObject.identifier() ?? "Unknown" | |
65 | } | |
66 | ||
67 | var useAlternateColor = false | |
68 | if let controlView = controlView as? NSTableView { | |
69 | useAlternateColor = controlView.isRowSelected(controlView.row(at: cellFrame.origin)) | |
70 | } | |
71 | ||
72 | var mainColor: NSColor? = textColor() | |
73 | if mainColor == nil { | |
74 | mainColor = useAlternateColor ? .alternateSelectedControlTextColor : .controlTextColor | |
75 | } | |
76 | ||
77 | var fadedColor = mainColor!.withAlphaComponent(0.50) | |
78 | var textDrawRect = titleRect(forBounds: cellFrame) | |
79 | ||
80 | var titleString = NSMutableAttributedString(string: nameString!) | |
81 | titleString.setAttributes(rankedStringIsName ? nameAttributes : detailAttributes, range: NSMakeRange(0, titleString.length)) | |
82 | ||
83 | if abbrString != nil && abbrString!.hasPrefix("QSActionMnemonic") { | |
84 | titleString.addAttribute(.foregroundColor, value: rankedStringIsName ? fadedColor : fadedColor.withAlphaComponent(0.8), range: NSMakeRange(0, titleString.length)) | |
85 | } else { | |
86 | var i = 0 | |
87 | var j = 0 | |
88 | var hits: [Int] = [] | |
89 | count = hitMask?. | |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | ||
95 | NSUInteger i = 0; | |
96 | NSUInteger j = 0; | |
97 | NSUInteger hits[[titleString length]]; | |
98 | NSUInteger count = [hitMask getIndexes:(NSUInteger *)&hits maxCount:[titleString length] inIndexRange:nil]; | |
99 | NSDictionary *attributes = @{ | |
100 | NSForegroundColorAttributeName: rankedStringIsName ? mainColor : fadedColor | |
101 | }; | |
102 | for(i = 0; i<count; i += j) { | |
103 | for (j = 1; i+j<count && hits[i+j-1] +1 == hits[i+j]; j++); | |
104 | [titleString addAttributes:attributes range:NSMakeRange(hits[i], j)]; | |
105 | } | |
106 | } else { | |
107 | [titleString addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithDouble:-1.0] range:NSMakeRange(0, [titleString length])]; | |
108 | } | |
109 | ||
110 | if (showDetails) { | |
111 | NSString *detailsString = [drawObject details]; | |
112 | ||
113 | NSRange returnRange = [detailsString rangeOfString:@"\n"]; | |
114 | if (returnRange.location != NSNotFound) { | |
115 | detailsString = [detailsString substringToIndex:returnRange.location]; | |
116 | } | |
117 | ||
118 | detailsAttributes = [detailsAttributes mutableCopy]; | |
119 | [detailsAttributes setValue:[NSColor grayColor] forKey:NSForegroundColorAttributeName]; | |
120 | ||
121 | if (detailsString && detailsString.length && ![detailsString isEqualToString:nameString]) { | |
122 | [titleString appendAttributedString:[[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%@",detailsString] attributes:detailsAttributes] autorelease]]; | |
123 | } | |
124 | } | |
125 | ||
126 | NSRect centerRect = rectFromSize([titleString size]); | |
127 | centerRect.size.width = NSWidth(textDrawRect); | |
128 | centerRect.size.height = MIN(NSHeight(textDrawRect), centerRect.size.height); | |
129 | [titleString drawInRect:centerRectInRect(centerRect, textDrawRect)]; | |
130 | } | |
131 | ||
132 | @end | |
133 | */ |